package com.qf.java2201.web.bk;

import com.alibaba.fastjson.JSON;
import com.qf.java2201.common.R;
import com.qf.java2201.pojo.HUser;
import com.qf.java2201.service.UserService;
import com.qf.java2201.service.impl.UserServiceImpl;
import com.qf.java2201.utils.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @date 2022/4/8
 * @desc
 */
@WebServlet("/bklogin")
public class BkLoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");


        HUser user = new HUser();
        user.setUname(username);
        //对密码进行加密
        String md5upwd = MD5Util.encodePwdByMD5(password);
        user.setUpwd(md5upwd);

        //
        UserService userService = new UserServiceImpl();
        //
        HUser currentUser = userService.login(user);
        //
        R<HUser> r = new R();


        if(currentUser!=null){
            //登录成功
            r.setCode(200);
            r.setMsg("登录成功");
            r.setData(currentUser);
            String jsonResult = JSON.toJSONString(r);

            resp.getWriter().println(jsonResult);
        }else{
            //登录失败
            r.setCode(500);
            r.setMsg("用户名或密码错误");
            String jsonResult = JSON.toJSONString(r);
            resp.getWriter().println(jsonResult);
        }


    }
}
